package com.example.android.ouactivity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.example.android.ouactivity.adapter.CommentAdapter;
import com.example.android.ouactivity.login_register.Login;
import com.example.android.ouactivity.model.Comment;
import com.example.android.ouactivity.model.User;
import com.example.android.ouactivity.utils.HttpUtils;
import com.example.android.ouactivity.utils.ReadUserInfo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

/**
 * 评论回复实现的Activity by chqing
 * add title and Home 11.17
 */
public class CommentReplyActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private List<Comment> datas = new ArrayList<Comment>();
    private CommentAdapter adapter;
    private User user ;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String result = msg.obj.toString();
            Gson gson = new Gson();
            datas = gson.fromJson(result,new TypeToken<List<Comment>>(){}.getType());
            initView();
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment_reply);
        //设置返回，及页面标题
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        if (actionBar != null) {
            actionBar.setTitle("我的回复");
        }
        ReadUserInfo read = new ReadUserInfo(this,"userInfo");
        if(read.hasFile()){
            user = read.getUserByFile();
            HttpUtils httpUtils = new HttpUtils();
            String url = "http://www.chqing.cn/comment?sty=my&user="+user.getId();
            httpUtils.getNewsJSON(url,handler);
        }else {
            Toast.makeText(CommentReplyActivity.this,"哎呀，貌似出现了bug",Toast.LENGTH_SHORT).show();
        }
    }

    public void initView(){

        //初始化RecyclerView
        recyclerView = (RecyclerView)findViewById(R.id.my_comm_reply);
        //创建默认的线性LayoutManager
        layoutManager = new LinearLayoutManager(CommentReplyActivity.this,LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(layoutManager);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        recyclerView.setHasFixedSize(true);
        //添加分割线
        recyclerView.addItemDecoration(new DividerItemDecoration(CommentReplyActivity.this,DividerItemDecoration.VERTICAL));
        //创建并设置Adapter
        adapter = new CommentAdapter(datas);
        recyclerView.setAdapter(adapter);
        //监听
        adapter.setOnItemClickListener(new CommentAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, Comment comm) {
                Intent comment = new Intent(CommentReplyActivity.this,CommentListActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("news",String.valueOf(comm.getNews()));
                comment.putExtras(bundle);
                startActivity(comment);
            }
        });
    }
    //左上角返回的监听
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
